
#include "TowerDuplicate.h"
#include "Duplicate/DuplicateMgr.h"
#include "Character/CreatureMgr.h"
#include "Common/TableData/MonsterMonsterCfg.h"
#include "Character/HuoYueDuPart.h"
TowerDuplicate::TowerDuplicate()
{
	m_byType = (int8_t)EDuplicateType::EDuplicateType_Tower;
}

TowerDuplicate::~TowerDuplicate()
{

}


//初始化
bool TowerDuplicate::Init(uint32_t nDuplicateID, uint32_t nSceneID, IBrushMonster *pBrush)
{
	return Duplicate::Init(nDuplicateID, nSceneID, pBrush); 
}
//
void TowerDuplicate::UnInit()
{
	Duplicate::UnInit();
}


//生物进入场景
void TowerDuplicate::OnCreatureEnter(Creature *pCreature)
{
	if (nullptr == pCreature)
	{
		return;
	}
	if (CREATURE_PLAYER == pCreature->CreatureKind())
	{
		InitExploit(pCreature->GetCid()); //初始化玩家功勋
		//进入副本时移除掉背包中 副本商店物品
		Part *pPackage = pCreature->GetPart(PART_PACKAGE);
		if (nullptr != pPackage)
		{
			SItemSourceParam soureParam;
			soureParam.source = S_Drop;
			pPackage->RemoveAllByType((int32_t)EItemSubType::EItemSubType_dup_shop, soureParam);
		}
		//进入副本，塔防活跃度
		HuoYueDuPart*pHuoyuePart = dynamic_cast<HuoYueDuPart*>(pCreature->GetPart(PART_HUOYUEDU));
		if (nullptr != pHuoyuePart)
		{
			pHuoyuePart->AddHuoYueDuRecord(HYDT_ACTIVITY_TOWER);
		}
		//LogDebugFmtPrint("[logic] TowerDuplicate::EnterDuplicate success...cid:%u ,duplicateid:%u, sceneid:%u ", pCreature->GetCid(), m_nDuplicateID, m_nSceneID);
	}
}

//生物离开副本
void TowerDuplicate::OnPlayerLeave(Player *pPlayer)
{
}

//是否能进入
bool TowerDuplicate::OnCanEnter(Creature *pCreature)
{
	pCreature = pCreature;
	return true;
}

//进入区域
bool TowerDuplicate::OnEnterArea(uint32_t nAreaID)
{
	nAreaID = nAreaID;
	return true;
}
//复活
void TowerDuplicate::OnRelive(Player *pPlayer, int8_t byType)
{
	pPlayer = pPlayer;
	byType = byType;
}

//获取功勋
int32_t TowerDuplicate::GetExploit(CharIDType charId)
{
	return GetCharExploit(charId);
}
//购买功勋商店物品  costExploit : 购买物品需要消耗的功勋
void TowerDuplicate::BuyExploitItem(CharIDType charId, int32_t costExploit)
{
	AddExploit(charId, -costExploit);
}
//主动离开副本之前 的处理 
void TowerDuplicate::OnActiveBeforeLeave(CharIDType cid)
{
	Player *pPlayer = GetPlayer(cid);
	if (nullptr != pPlayer)
	{
		Part *pPackage = pPlayer->GetPart(PART_PACKAGE);
		if (nullptr != pPackage)
		{
			SItemSourceParam sourceParam;
			sourceParam.source = S_Drop;
			pPackage->RemoveAllByType((int32_t)EItemSubType::EItemSubType_dup_shop, sourceParam);
		}
		DelExploit(cid);
	}
	Duplicate::OnActiveBeforeLeave(cid);
}
//定时器更新
void TowerDuplicate::OnTimerUpdate(uint32_t nTimerID)
{
	switch (nTimerID)
	{
	case ETimer_Flop:
		{
			//翻牌结束
			OnCommon_FlopStateEnd();
		}
		break;
	case ETimer_Clean:
		{
			OnCommon_CleanStateEnd();
		}
		break;
	case ETimer_Destory:
		{
			LogDebugFmtPrint("[logic] TowerDuplicate destory .... duplicateid:%u , sceneid:%u, mapid:%u ", m_nDuplicateID, m_nSceneID, m_nMapID);
			//回收
			OnCommon_DestoryStateEnd();
		}
		break;
	default:
		break;
	}

}
//执行事件
void TowerDuplicate::OnExecuteEvent(uint16_t nEventID, uint32_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_CREATURE_DEAD:
	{
		if (CREATURE_MONSTER == bySrcType)
		{
			DieEvent *pDieEvent = (DieEvent*)pContext;
			if (pDieEvent->monsterId > 0)
			{
				Player *pPlayer = GetPlayer(pDieEvent->killerCid);
				if (nullptr != pPlayer)
				{
					const MonsterMonsterCfgInfo *pMonCfg = g_GetMonsterMonsterCfgTable()->GetMonsterMonsterCfgInfo(pDieEvent->monsterId);
					if (nullptr != pMonCfg)
					{
						AddExploit(pDieEvent->killerCid, pMonCfg->specialPropertyValue);
					}
				}
				
			}
			
		}
		
	}
	break;
	case EVENT_ENTER_SCENE:
	{
	}
	break;
	case EVENT_DISCONNECT:
	{
	}
	break;
	}
}

void TowerDuplicate::OnReady_State()
{

}

void TowerDuplicate::OnBegin_State()
{
	OnCommon_DupBeginState();
}

void TowerDuplicate::OnFinish_State()
{
	OnCommon_DupFinishState();
}

void TowerDuplicate::OnFlop_State()
{
	//设置抽奖时间 15s
	LogDebugFmtPrint("[logic] TowerDuplicate::OnDuplicateState_Flop....duplicateid:%u , sceneid:%u ", m_nDuplicateID, m_nSceneID);
	OnCommon_DupFlopState();
}

void TowerDuplicate::OnClean_State()
{
	OnCommon_DupCleanState();
}

void TowerDuplicate::OnDestory_State()
{
	OnCommon_DupDestoryState();
}